import pymysql

if __name__ == '__main__':

    # 1、读取文件
    with open("../../data/students.txt", mode="r", encoding="UTF-8") as file:
        studnets = [line.rstrip() for line in file.readlines()]

    # 2、创建数据库连接
    con = pymysql.connect(
        host="master",
        port=3306,
        user="root",
        password="123456",
        database="shujia"
    )
    cursor = con.cursor()

    sql = "insert into students values(%s,%s,%s,%s,%s)"

    # 3、循环将数据写入数据库
    for stu in studnets:
        student = stu.split(",")
        cursor.execute(sql, student)

    # 4、提交事务
    con.commit()
    con.close()
